﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace XBoshiGFS.forms
{
    /// <summary>
    /// 支渠信息维护界面
    /// </summary>
    public partial class SegmentForm : Form
    {
  
        public SegmentCourseSet segment { get; set; }

        public XBoshiGFSModelContainer context = new XBoshiGFSModelContainer();  

        public SegmentForm()
        {
            InitializeComponent();

            segment = new SegmentCourseSet();
            segment.Id = Guid.NewGuid().ToString();
            segment.strName ="新建支渠"; 
            context.AddToSegmentCourseSet(segment);
            segmentCourseSetBindingSource.DataSource = segment;
            cmbStation.DataSource = (from p in context.StationSet select p);

        }
         
        
        public SegmentForm(StationSet   p_station)
        {
            InitializeComponent();


            var station = (from p in context.StationSet  where p.Id == p_station.Id select p).FirstOrDefault();


            segment = new SegmentCourseSet();
            segment.Id = Guid.NewGuid().ToString();
            segment.strName = "新建支渠";
            segment.StationSet = station; 
            context.AddToSegmentCourseSet(segment);
            segmentCourseSetBindingSource.DataSource = segment;



            //  var station = segment.StationSet; 
            cmbStation.DataSource = (from p in context.StationSet select p);
            cmbStation.Enabled = false;
        }


         public SegmentForm(SegmentCourseSet p_segment)
        {
            InitializeComponent();
            segment = (from p in context.SegmentCourseSet where p.Id == p_segment.Id select p).FirstOrDefault();
            context.Attach(segment);
            segmentCourseSetBindingSource.DataSource = segment;

            cmbStation.DataSource = (from p in context.StationSet select p);
            cmbStation.Enabled = false;
        }
 
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (TBName.Text == "")
            {
                MessageBox.Show("请填写名称！", "提示");
                TBName.Focus();
                return;
            }
            if (TBCode.Text == "")
            {
                MessageBox.Show("请填写代码！", "提示");
                TBCode.Focus();
                return;
            }

            if (String.IsNullOrWhiteSpace(segment.StationId ))
            {
                MessageBox.Show("请选择上层单位！", "提示");
                return;
            }

            var code = (from p in context.SegmentCourseSet  where p.strCode == TBCode.Text select p);
            var maxCode = "";
            try
            {
                var sugesstNo = (from p in context.SegmentCourseSet select p.strCode).Cast<Int32>().Max();
                maxCode = String.Format("{0}", (sugesstNo + 1)); //(int.Parse(sugest.FirstOrDefault().ToString()) + 1).ToString();
            }
            catch
            {
            } 
            
             if (code.Count() == 0)
            {
            }
            else
            {
                if (code.Count() == 1 && code.First().Id == segment.Id)
                {
                }
                else
                {
                    MessageBox.Show("代码重复！建议编码为：" + maxCode, "提示");
                    TBCode.Focus();
                    return;
                }
            }
 
                //设置sc的相关信息
            if (segment.EntityKey == null)
            {

                context.SegmentCourseSet.AddObject(segment);
            }
            else
            {
             //   segment.Id = Guid.NewGuid().ToString(); 
             //    context.SegmentCourseSet.Attach(segment);
            }
            context.SaveChanges();
            this.DialogResult = DialogResult.Yes;  
 
        }

        private void SegmentForm_Load(object sender, EventArgs e)
        {

        }

        private void cmbStation_SelectionChangeCommitted(object sender, EventArgs e)
        {
            var strID = ((ComboBox)sender).SelectedValue;
            segment.StationId = strID.ToString(); 
        }

    
    }
}
